"""
Case Type   : 系统内部使用工具
Case Name   : gs_ctl stop使用-D指定正确数据库实例目录备机是否可以关闭成功
Create At   : 2021/01/19
Owner       : opentestcase015
Description :
    1.查看集群状态
    2.gs_ctl指定-D设置参数为不正确的数据库实例目录，stop关闭备机
    3.查看备机状态
    4.环境恢复
Expect      :
    1.正常
    2.关闭失败
    3.备机为Normal状态
    4.成功
History     :
    Modified by lopentestcase027 2023/1/12: 优化用例步骤格式及一主两备判断方式
"""

import unittest
import os
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

LOG = Logger()
primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class LogicalReplication(unittest.TestCase):
    def setUp(self):
        LOG.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.sh_standby = CommonSH('Standby1DbUser')

    def test_system_internal_tools(self):
        LOG.info('--------step1:查看集群状态   expect:正常---------')
        status = self.sh_standby.get_db_cluster_status(param='status')
        self.assertTrue(status)
        LOG.info('--------step2:关闭数据库备机  expect:关闭失败---------')
        msg = self.sh_standby.stop_db_instance(dn_path=macro.DB_BACKUP_PATH)
        self.assertIn(self.constant.NOT_EXIST, msg)
        LOG.info('--------step3:查看备机状态   expect:备机为Normal状态-------')
        msg = self.sh_standby.execute_gsctl("query", "Normal")
        self.assertTrue(msg)

    def tearDown(self):
        LOG.info('--------step4:环境恢复    expect:成功---------')
        restart = self.sh_standby.restart_db_cluster()
        LOG.info(restart)
        status = self.sh_standby.get_db_cluster_status(param='status')
        self.assertTrue(status)
        LOG.info(f'--------{os.path.basename(__file__)} end--------')